<?php
/**
 * 出租管理
 */
namespace Member\Controller;
use Org\Yike\Member;
class RentalController extends CommonController {
    public function _initialize(){
        parent::_initialize();
        $this->is_login();
        $this->status = getStatus('rental');
        $this->menu = getLinkMenuFor(array(2,8,10,17,20,21,27,26,25,23,24),1);
    }

    public function index(){
        $member = new Member();
        $list = $member::getRentalList(20,'r.status!=4 and r.user_id='.$this->user['id'],true);
        $this->list = $list;
        $this->seoKey(array('seo_title'=>'出租房源'));
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data = $_POST['data'];
            $data['house_type'] = $_POST['house_type'];
            $data['mating'] = $_POST['mating'];
            $data['metro'] = intval($_POST['metro']);
            $info = strip_tags($data['info']);
            if(empty($data['title'])){
                $this->error('请填写出租房源名称');
            }
            if(empty($info)){
                $this->error('请填写出租房源内容');
            }
            $content = stripslashes($info);
            $data['info'] = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
            $data['info'] = addslashes($data['info']);
            $data['user_id'] = $this->user['id'];
            $data['add_time']  = time();
            $data['info'] = preg_replace('/<a[^>]*>|<\/a>/isU','',$data['info']);
            $data['house_type'] = !empty($data['house_type']) ? '|'.implode('|',$data['house_type']).'|' : '';
            $data['mating'] = !empty($data['mating']) ? '|'.implode('|',$data['mating']).'|' : '';
            $data['renovation_type'] = intval($_POST['renovation_type']);
            $data['orientations'] = intval($_POST['orientations']);
            $data = $this->_insertPic($data);
            if($data['status'] !=5){
                $data['status'] = C('RENTAL_STATUS');
            }
            M('rental')->create($data);
            if(M('rental')->add($data)){
                if($data['status'] == 3){
                    $str = '发布成功,请等候管理员审核';
                }elseif($data['status'] == 1){
                    $str = '出租房源发布成功';
                }elseif($data['status'] == 5){
                    $str = '已存为草稿';
                }else{
                    $str = '出租房源发布成功';
                }
                $this->success($str);
            }else{
                $this->error('出租房源发布失败');
            }
        }else{
            $member = new Member();
            $this->cate_list = $member::getCate('area_cate',1);
            $this->seoKey(array('seo_title'=>'发布出租房源'));
            $this->display();
        }
    }

    public function edit(){
        if(IS_POST){
            $id = intval($_POST['id']);
            $data = $_POST['data'];
            $data['house_type'] = $_POST['house_type'];
            $data['mating'] = $_POST['mating'];
            $data['metro'] = intval($_POST['metro']);
            $info = strip_tags($data['info']);
            if(empty($data['title'])){
                $this->error('请填写出租房源名称');
            }
            if(empty($info)){
                $this->error('请填写出租房源内容');
            }
            $content = stripslashes($info);
            $data['info'] = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
            $data['info'] = addslashes($data['info']);
            $data['info'] = preg_replace('/<a[^>]*>|<\/a>/isU','',$data['info']);
            $data['house_type'] = !empty($data['house_type']) ? '|'.implode('|',$data['house_type']).'|' : '';
            $data['mating'] = !empty($data['mating']) ? '|'.implode('|',$data['mating']).'|' : '';
            $data['renovation_type'] = intval($_POST['renovation_type']);
            $data['orientations'] = intval($_POST['orientations']);
            $data = $this->_insertPic($data);
            if($data['status'] !=5){
                $data['status'] = C('RENTAL_STATUS');
            }
            if(M('rental')->where(array('id'=>$id,'user_id'=>$this->user['id']))->save($data)){
                if($data['status'] == 3){
                    $str = '发布成功,请等候管理员审核';
                }elseif($data['status'] == 1){
                    $str = '出租房源发布成功';
                }elseif($data['status'] == 5){
                    $str = '已存为草稿';
                }else{
                    $str = '出租房源发布成功';
                }
                $this->success($str);
            }else{
                $this->error('出租房源发布失败');
            }
        }else{
            $info = M('rental')->where(array('user_id'=>$this->user['id'],'id'=>intval($_GET['id'])))->find();
            if(!$info){
                $this->error('找不到对应的房源信息');
            }
            $member = new Member();
            $this->cate_list = $member::getCate('area_cate',1);

            //获取小区
            $ids = getCateIds($info['area_id']);
            $estate_list = M('estate')->where(array('status'=>1,'area_id'=>array('in',$ids)))->field('id,title')->order('id desc')->select();
            $this->assign('estate_list',$estate_list);
            $this->assign('img_list',json_decode($info['file'],true));
            $this->assign('house_type',explode('|',trim($info['house_type'],'|')));
            $this->assign('mating',explode('|',trim($info['mating'],'|')));
            $this->assign('info',$info);
            $this->seoKey(array('seo_title'=>'编辑出租房源'));
            $this->display();
        }
    }

    public function message(){
        $member = new Member();
        $list = $member::getRentalMess(20,'orr.store_id='.$this->user['id'],true);
        $this->list = $list;
        $this->type = order_type('rental');
        $this->seoKey(array('seo_title'=>'出租房留言'));
        $this->display();
    }

    //留言详细
    public function editMess(){
        $id = intval($_GET['id']) ? intval($_GET['id']) : intval($_POST['id']);
        $order = M('order_rental')->where(array('id'=>$id,'store_id='=>$this->user['id']))->find();
        if(!$order['id']){
            $this->error('找不到对应的房源信息');
        }
        if($_POST){
            $data = array('info'=>strip_tags($_POST['info']),'status'=>intval($_POST['status']));
            $res = M('order_rental')->where(array('id'=>$id,'store_id='=>$this->user['id']))->save($data);
            if($res){
                $this->success("留言保存成功");
            }else{
                $this->error("留言保存失败");
            }
        }else{
            $this->rental = M('rental')->where(array('id'=>$order['rental_id']))->field('title')->find();
            $this->order = $order;
            $this->seoKey(array('seo_title'=>'出租房源留言详细'));
            $this->display();
        }
    }

    public function del(){
        $info = M('rental')->where(array('user_id'=>$this->user['id'],'id'=>intval($_GET['id'])))->find();
        if(!$info){
            $this->error('找不到对应的房源信息');
        }
        if(M('rental')->where(array('id'=>$info['id']))->save(array('status'=>4))){
            $this->success('房源删除成功!');
        }else{
            $this->error('房源删除失败');
        }
    }




}